import md5 from 'md5';

const getQueryString = name => {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  const r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return decodeURIComponent(r[2]);
  }
  return null;
};

const secretPassword = pass => {
  return md5(pass) + new Date().getTime();
};

const randomString = (len = 12) => {
  const CHARS = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';
  let pwd = '';
  for (let i = 0; i < len; i++) {
    pwd += CHARS.charAt(Math.floor(Math.random() * CHARS.length));
  }
  return pwd;
};

const filterEmptyStr = obj => {
  const res = {};
  Object.keys(obj).forEach(key => {
    if (obj[key] !== '') {
      res[key] = obj[key];
    }
  });
  return res;
};

export default {
  getQueryString,
  secretPassword,
  randomString,
  filterEmptyStr
};
